package com.ybfenpan.service.shop;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.ybfenpan.model.Member;
import com.ybfenpan.model.ParamWage;
import com.ybfenpan.model.log.LogEle;
import com.ybfenpan.model.log.LogMoney;
import com.ybfenpan.model.shop.Orders;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.service.ParamWageService;
import com.ybfenpan.service.WageCurrentService;
import com.ybfenpan.service.core.BaseService;
import com.ybfenpan.service.log.LogEleService;
import com.ybfenpan.service.log.LogMoneyService;
import com.ybfenpan.service.log.LogShuiService;
import org.springframework.stereotype.Service;






@Service
public class OrdersService extends BaseService {

	@Resource
	private MemberService memberService;
	@Resource
	private LogMoneyService logMoneyService;
	@Resource

	private GoodsService goodsService;
	@Resource
	private ParamWageService paramWageService;
	@Resource
	private LogEleService logEleService;
	@Resource
	private OrdersService ordersService;
	@Resource
	private WageCurrentService wageCurrentService;

	/**
	 * 保存
	 * 
	 * @param member
	 */
	public void saveweiyi(Member member, Orders orders, Integer payWay, Double payPrice, String orderNo, Integer goodsId, Integer type, Integer num) throws InterruptedException {

		member = memberService.getByKey(member.getId());
		this.ordersService.saveOrUpdate(orders);

			// 余额
			if (member.getEle() >= payPrice) {
				System.out.println("payPrice:" + payPrice);
				Map<String, Object> mapMember1 = new HashMap();
				mapMember1.put("id", member.getId());
				mapMember1.put("diyUpdate", "money=money-" + payPrice);

				// 更新余额
				memberService.update(mapMember1);
				member = memberService.getByKey(member.getId());
				Double yu = member.getMoney();


				// 新增电子积分记录
				LogMoney logMoney = new LogMoney();
				logMoney.setMemberId(member.getId());
				logMoney.setMemberLoginName(member.getLoginName());
				logMoney.setTitle("购买产品，订单号：" + orderNo);
				logMoney.setState(88);
				logMoney.setNum(-payPrice);
				logMoney.setYu(yu);
				logMoneyService.saveOrUpdate(logMoney);
				this.ordersService.saveOrUpdate(orders);
				Map<String, Object> mapGoods = new HashMap();
				mapGoods.put("id", goodsId);
				mapGoods.put("diyUpdate", "totalNum=totalNum-" + orders.getBuyNum() + ",sellNum=sellNum+" + orders.getBuyNum());
				this.goodsService.update(mapGoods);

			}
	}

	/**
	 * 保存
	 * 
	 * @param
	 */
	public void updateNo(Orders orders) throws InterruptedException {
		if (orders.getOrderState() == 0) {

			Map mapOrders = new HashMap();
			mapOrders.put("id", orders.getId());
			mapOrders.put("orderState", 2);
			this.ordersService.update(mapOrders);
			Integer payWay = orders.getPayWay();
			Double payPrice = orders.getPayPrice();
			String orderNo = orders.getOrderNo();
			if (payWay == 10) {

				Map<String, Object> mapMember = new HashMap();
				mapMember.put("id", orders.getMemberId());
				mapMember.put("diyUpdate", "money=money+" + payPrice);

				// 更新余额
				memberService.update(mapMember);
				Member member = memberService.getByKey(orders.getMemberId());
				Double yu = member.getMoney();

				// 新增记录
				LogMoney logMoney = new LogMoney();
				logMoney.setMemberId(member.getId());
				logMoney.setMemberLoginName(member.getLoginName());
				logMoney.setTitle("取消订单，订单号：" + orderNo);
				logMoney.setState(88);
				logMoney.setNum(payPrice);
				logMoney.setYu(yu);
				logMoneyService.saveOrUpdate(logMoney);

			}

		}

	}
}
